package org.sixtythousand.joker.netty;

import io.netty.handler.codec.http.FullHttpRequest;
import org.sixtythousand.joker.servlet.SimpleResponse;

import javax.servlet.ServletOutputStream;
import java.io.IOException;
import java.io.OutputStream;

/**
 * @author zhoulk
 * @date 2023/10/17 17:51
 * @desc
 */
public class NettyHttpResponse extends SimpleResponse {
    protected FullHttpRequest fullHttpRequest;
    protected NettyHttpChannel nettyHttpChannel;
    protected ServletOutputStreamWrapper outputStreamWrapper;


    public NettyHttpResponse(NettyHttpRequest request) {
        super(request);
        this.fullHttpRequest = request.getFullHttpRequest();
        this.outputStreamWrapper = new ServletOutputStreamWrapper();
        this.nettyHttpChannel = (NettyHttpChannel)request.getChannel();
        NettyServletOutputStream outputStream = new NettyServletOutputStream(nettyHttpChannel);
        this.outputStreamWrapper.addSource(outputStream);
    }


    @Override
    public ServletOutputStream getOutputStream() throws IOException {
        return outputStreamWrapper;
    }


    @Override
    public void flushBuffer() throws IOException {
        outputStreamWrapper.flush();
    }


}
